package com.iweb.dao.impl;

import com.iweb.dao.MerchantDao;
import com.iweb.pojo.Merchant;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author: jack
 * @date: 2022-08-05 0005
 * @ApiNOTE:
 */
public class MerchantDaoImpl implements MerchantDao {

  private DataSource dataSource;

  public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
  }

  @Override
  public Merchant selectById(Integer merchantId) {
    Merchant merchant = null;
    Connection connection = null;
    try {

      connection = dataSource.getConnection();
      PreparedStatement pst
              = connection.prepareStatement("select * from tb_merchant where mid = ?");

      pst.setInt(1, merchantId);

      ResultSet rs = pst.executeQuery();

      while (rs.next()) {
        merchant = new Merchant(
                rs.getInt(1),
                rs.getString(2),
                rs.getString(3),
                rs.getString(4), rs.getString(5),
                rs.getDouble(6),
                rs.getString(7),
                rs.getDouble(8),
                rs.getDouble(9),
                rs.getInt(10),
                rs.getInt(11), rs.getInt(12)
        );
      }
    } catch (Exception e) {
      e.printStackTrace();
    }finally {
      try {
        connection.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
    return merchant;
  }
}
